using System;
using System.ComponentModel.DataAnnotations;
using Volo.Abp;
using Volo.Abp.Domain.Entities.Auditing;
using Volo.Abp.MultiTenancy;

namespace UserManagementService.Organizations;

/// <summary>
/// 组织成员实体
/// </summary>
public class OrganizationMember : FullAuditedEntity<Guid>, IMultiTenant
{
    /// <summary>
    /// 租户ID
    /// </summary>
    public Guid? TenantId { get; set; }

    /// <summary>
    /// 组织ID
    /// </summary>
    public Guid OrganizationId { get; set; }

    /// <summary>
    /// 用户ID
    /// </summary>
    public Guid UserId { get; set; }

    /// <summary>
    /// 职位
    /// </summary>
    [StringLength(100)]
    public string? Position { get; set; }

    /// <summary>
    /// 描述
    /// </summary>
    [StringLength(500)]
    public string? Description { get; set; }

    /// <summary>
    /// 是否为主管
    /// </summary>
    public bool IsManager { get; set; }

    /// <summary>
    /// 加入时间
    /// </summary>
    public DateTime JoinTime { get; set; }

    /// <summary>
    /// 离开时间
    /// </summary>
    public DateTime? LeaveTime { get; set; }

    /// <summary>
    /// 是否在职
    /// </summary>
    public bool IsActive { get; set; } = true;

    /// <summary>
    /// 扩展属性（JSON格式）
    /// </summary>
    public string? Properties { get; set; }

    /// <summary>
    /// 关联的组织
    /// </summary>
    public virtual Organization Organization { get; set; } = null!;

    protected OrganizationMember()
    {
    }

    public OrganizationMember(
        Guid id,
        Guid organizationId,
        Guid userId,
        string? position = null,
        string? description = null)
        : base(id)
    {
        OrganizationId = organizationId;
        UserId = userId;
        Position = position;
        Description = description;
        JoinTime = DateTime.Now;
        IsActive = true;
    }

    /// <summary>
    /// 更新成员信息
    /// </summary>
    public OrganizationMember UpdateInfo(string? position = null, string? description = null)
    {
        Position = position;
        Description = description;

        return this;
    }

    /// <summary>
    /// 设置主管身份
    /// </summary>
    public OrganizationMember SetManager(bool isManager)
    {
        IsManager = isManager;

        return this;
    }

    /// <summary>
    /// 激活成员
    /// </summary>
    public OrganizationMember Activate()
    {
        IsActive = true;
        LeaveTime = null;

        return this;
    }

    /// <summary>
    /// 停用成员
    /// </summary>
    public OrganizationMember Deactivate()
    {
        IsActive = false;
        LeaveTime = DateTime.Now;

        return this;
    }

    /// <summary>
    /// 设置扩展属性
    /// </summary>
    public OrganizationMember SetProperties(string? properties)
    {
        Properties = properties;

        return this;
    }
}